package com.tangyee.home_exercise_diet.activities;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangyee.home_exercise_diet.fragment.FoodOrExerciseListFragment;
import com.tangyee.R;
import com.tangyee.home_exercise_diet.beans.FoodOrExercise;
import com.tangyee.home_exercise_diet.beans.TypeEnum;
import com.tangyee.home_exercise_diet.dao.FoodOrExerciseHolder;
import com.tangyee.home_exercise_diet.fragment.FoodOrExerciseListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加记录的Activity，就一个Tablayout、一个ViewPager托管了两个Fragment（一个常见，一个收藏）
 */
public class AddFoodOrExerciseActivity extends AppCompatActivity {

    public static final String TYPE = "type";
    public static final String DATE = "date";

    private TabLayout tab;
    private ViewPager pager;
    private List<String> list;

    private ImageView back;
    private ImageView save;

    private TypeEnum type;
    private String date;

    private TextView title;

    private FoodOrExerciseListFragment normal, collect;

    private List<FoodOrExercise> preSelectedList;
    private List<FoodOrExercise> selectedList = new ArrayList<>();

    private int num = 0;
    private TextView number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exercise_and_diet_activity_add);

        date = getIntent().getStringExtra(DATE);
        type = TypeEnum.valueOf(getIntent().getStringExtra(TYPE));

        this.title = findViewById(R.id.center_text);
        title.setText(type.getTypeName());

        this.number = findViewById(R.id.number);
        number.setVisibility(View.GONE);

        this.back = findViewById(R.id.left_img);
        back.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        this.save = findViewById(R.id.right_img);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveSelected();
                onBackPressed();
            }
        });

        //初始化界面
        this.tab = findViewById(R.id.add_food_tab);
        this.pager = findViewById(R.id.add_food_pager);
        //初始化数据
        list = new ArrayList<>();
        list.add("常见");
        list.add("收藏");
        normal = FoodOrExerciseListFragment.newInstance(date, type.name(), "常见");
        collect = FoodOrExerciseListFragment.newInstance(date, type.name(),"收藏");

        preSelectedList = FoodOrExerciseHolder.get(getApplicationContext()).getListByDateAndType(date, type.name());

        //设置Adapter
        pager.setAdapter(new BloodSugarFragmentAdapter(getSupportFragmentManager(), list));

        //Tab与ViewPager绑定
        tab.setupWithViewPager(pager);

        //设置缓存2个界面
        pager.setOffscreenPageLimit(2);

    }

    public List<FoodOrExercise> getPreSelectedList() {
        return preSelectedList;
    }

    public List<FoodOrExercise> getSelectedList() {
        return selectedList;
    }

    public void updateNum(boolean isAdd){
        if (isAdd)
            num ++;
        else
            num --;
        if (this.num <= 0){
            this.num = 0;
            number.setVisibility(View.GONE);
        } else {
            this.number.setText(num + "");
            number.setVisibility(View.VISIBLE);
        }
    }

    public void saveSelected(){
        for (int i = 0; i < this.selectedList.size(); i ++) {
            FoodOrExercise foodOrExercise = this.selectedList.get(i);
            foodOrExercise.setType(type);
            foodOrExercise.setDate(date);
            FoodOrExerciseHolder.get(getApplicationContext()).insertRecord(foodOrExercise);
        }
        for (int i = 0; i < this.preSelectedList.size(); i ++) {
            FoodOrExercise foodOrExercise = this.preSelectedList.get(i);
            FoodOrExerciseHolder.get(getApplicationContext()).updateRecord(foodOrExercise);
        }
    }

    class BloodSugarFragmentAdapter extends FragmentPagerAdapter {
        private List<String> list;

        public BloodSugarFragmentAdapter(FragmentManager fm, List<String> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0){
                return normal;
            } else if (position == 1){
                return collect;
            }
            return null;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position);
        }
    }

}
